from queue import Queue
from threading import Thread

import requests
from lxml import etree
import time


def task():
    global counter, task_queue
    while task_queue.qsize() > 0:
        page = task_queue.get()
        url = f'https://book.zongheng.com/store/c0/c0/b0/u4/p{page}/v0/s9/t0/u0/i1/ALL.html'
        response = requests.get(url)
        html_str = response.content.decode()
        root = etree.HTML(html_str)
        story_list = root.xpath("//div[@class='bookinfo']")
        for story in story_list:
            author = story_type = " "
            book_name = "".join(story.xpath(".//div[@class='bookname']/a/text()"))
            target = story.xpath(".//div[@class='bookilnk']/a/text()")
            if len(target) == 2:
                author = target[0]
                story_type = target[1]
            profile = "".join(story.xpath(".//div[@class='bookintro']/text()"))
            story_link = "".join(story.xpath("../div[@class='bookimg']/a/@href"))
            counter += 1
            print(f"{counter} 书名:{book_name}, 作者:{author}, 类型:{story_type}, 简介:{profile}, 链接:{story_link}")


if __name__ == '__main__':
    counter = 0
    task_queue = Queue()
    for page in range(1, 143 + 1):
        task_queue.put(page)

    start_time = time.time()
    thead_list = [Thread(target=task) for _ in range(16)]
    for t in thead_list:
        t.start()
    for t in thead_list:
        t.join()

    print("耗时:", time.time() - start_time)
